<template>
    <div class="app-container">
        <el-form ref="integralGrade" :model="integralGrade" label-width="120px">
            <el-form-item label="借贷额度">
                <el-input-number v-model="integralGrade.borrowAmount" :min="0" />
            </el-form-item>
            <el-form-item label="积分区间开始">
                <el-input-number v-model="integralGrade.integralStart" :min="0" />
            </el-form-item>
            <el-form-item label="积分区间结束">
                <el-input-number v-model="integralGrade.integralEnd" :min="0" />
            </el-form-item>
            <el-form-item>
                <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
                <el-button type="danger" @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import integralGradeApi from '@/api/core/integral-grade'

export default {
    data() {
        return {
            integralGrade: this.initIntegralGrade(), // 初始化数据
            originalIntegralGrade: {}, // 深拷贝原始数据
            saveBtnDisabled: false, // 保存按钮是否禁用，防止表单重复提交
        }
    },

    // 页面渲染成功
    created() {
        if (this.$route.params.id) {
            this.fetchDataById(this.$route.params.id)
        } else {
            this.originalIntegralGrade = this.initIntegralGrade(); // 初始化方法
        }
    },

    methods: {
        initIntegralGrade() {
            return {
                id: null,
                borrowAmount: '',
                integralStart: '',
                integralEnd: ''
            };
        },
        saveOrUpdate() {
            // 禁用保存按钮
            this.saveBtnDisabled = true
            if (!this.integralGrade.id) {
                this.saveData()
            } else {
                this.updateData()
            }
        },

        // 新增数据
        saveData() {
            integralGradeApi.save(this.integralGrade).then((response) => {
                this.$message({
                    type: 'success',
                    message: response.message,
                })
                this.$router.push('/core/integral-grade/list')
            })
        },

        // 根据id查询记录
        fetchDataById(id) {
            integralGradeApi.getById(id).then(response => {
                this.integralGrade = response.data.integralGrade
                this.originalIntegralGrade = JSON.parse(JSON.stringify(this.integralGrade));
            })
        },

        // 根据id更新记录
        updateData() {
            integralGradeApi.updateById(this.integralGrade).then(response => {
                this.$message({
                    type: 'success',
                    message: response.message
                })
                this.$router.push('/core/integral-grade/list')
            })
        },
        resetForm() {
            this.$refs.integralGrade.resetFields();
            this.integralGrade = JSON.parse(JSON.stringify(this.originalIntegralGrade));
        }
    },
}
</script>